<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/4
 * Time: 14:40
 */

namespace app\admin\controller;

use org\Auth;
use think\Controller;
use think\Db;
use think\Request;

class base extends Controller
{

    protected $model, $order, $sort;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        if (!$this->getLoginUser()) {
            return $this->redirect('pub/login');
        }

        if (empty(cookie('think_var_admin'))) {

            switch (cookie('think_var_admin')) {
                case 'en':
                    cookie('think_var_admin', 'en-us');
                    break;
                case 'cn':
                default:
                    cookie('think_var_admin', 'zh-cn');
                    break;
                //其它语言
            }
        }
        $this->assign('Admin', $this->getLoginUser());
        $this->assign('Massagecount', db('Massage')->where('status',0)->where('lang',cookie('think_var_admin'))->where('cid',0)->count('id'));
        $this->_html_empty();

        //超级管理员拥有所有权限
        if ($this->getLoginUser()->id == 1) {
            return TRUE;
        }

        $auth = new Auth();
        $authlist =  $this->request->module().'/'.$this->request->controller().'/'.$this->request->action();

        if (!$auth->check($this->request->module() . '/' . $this->request->controller() . '/' . $this->request->action(), $this->getLoginUser()->id)) {
            $this->error('你没有权限'.$authlist, 'admin/index/welcome');
        }
    }

    public function _initialize()
    {

        $this->model = model($this->request->controller());

        // 排序字段 默认为主键名
        $this->order = $this->request->param('order') ? $this->request->param('order') : 'id';

        // 接受 sort参数 0 表示倒序 非0都 表示正序
        $this->sort = ($this->request->param('sort') == 'asc' ? 'asc' : 'desc');

    }

    public function getLoginUser()
    {
        $Admin = session('Admin', '', 'admin');
        if ($Admin && $Admin->id) {
            return $Admin;
        }
        return false;
    }


    public function index()
    {
        $this->assign('en', $this->cheaklangnav('en-us'));
        $this->assign('zh', $this->cheaklangnav('zh-cn'));

        $order[$this->order] = $this->sort;
        $list = $this->model->page_index($this->datas, $order)->appends($this->request->param());
        $this->assign('num', $this->model->where($this->datas)->count());
        $this->assign('list', $list);
        $this->assign('nav', $this->cheaklangnav(cookie('think_var_admin')));
        $this->assign('addcid', $this->request->param('cid') ? $this->request->param('cid') : '');
        return $this->fetch();
    }

    /**
     * 查看语言分类
     */
    public function cheaklangnav($lang)
    {
        // $d = model('nav')->where('model', $this->request->controller())->where('lang', $lang)->cache()->select();
        $d = allow_release_arctype(0, array($this->request->controller()),$lang,false);
        // dump($d);
        return $d;
    }

    /**
     * 添加
     */
    public function add()
    {

            $ss = allow_release_arctype(0, array($this->request->controller()));
            $this->assign('ss', $ss);

//        echo '<br><br><br><br><br><br><br>'.cookie('think_var_admin');
        return $this->fetch('edit',
            ['addcid' => $this->request->param('addcid'),]
        );
    }

    /**
     * 编辑
     */
    public function edit($id)
    {

        $ss = allow_release_arctype(0,array($this->request->controller()));
        $this->assign('ss',$ss);

        if (intval($id) < 1) {
            $this->error('数据错误');
        }
        //清理缓存
        cache($this->request->controller().$id,null);
        $data = $this->model->get($id);
        $this->assign('data', $data);
        return $this->fetch();
    }


    /**
     * 修改
     */
    public function update($data)
    {
        $res = $this->model->allowField(true)->save($data, ['id' => intval($data['id'])]);
        if ($res) {
            return ajax_info('修改成功！', 'u');
        } else {
            return ajax_info('没有修改！');
        }
    }

    /**
     * 删除
     */
    public function del($id)
    {
        if ($this->request->isAjax()) {

            $re = $this->model->del($id);
            if ($re) {
                $this->_delcahe();
                ajaxMsg('1', '删除成功');
            } else {
                ajaxMsg('0', '删除成功');

            }
        }else{
            $re = $this->model->del($id);
            if ($re) {
                $this->_delcahe();
                $this->success('删除成功');
            } else {

                $this->error('删除失败');
            }
        }

    }


    /**
     * 排序
     */
    public function sort()
    {
        if ($this->request->isAjax()) {
            $result = $this->model->save(['sort' => $this->request->post('sort')], ['id' => $this->request->post('id')]);
            if ($result) {
                $this->_delcahe();
                $this->success('修改成功！');
            } else {
                $this->error('没有修改！');
            }
        }
    }

    /**
     * 恢复
     */
    public function resume()
    {
        if ($this->request->isAjax()) {
            $result = $this->model->save(['status' => '1'], ['id' => $this->request->post('id')]);
            if ($result) {
                $this->_delcahe();
                $this->success('修改成功！');
            } else {
                $this->error('没有修改！');
            }
        }
    }

    /**
     * 禁用
     */
    public function forbid()
    {
        if ($this->request->isAjax()) {
            $result = $this->model->save(['status' => '0'], ['id' => $this->request->post('id')]);
            if ($result) {
                $this->_delcahe();
                $this->success('修改成功！');
            } else {
                $this->error('没有修改！');
            }
        }
    }


    /**
     * 推荐
     */
    public function Rrecommended()
    {
        if ($this->request->isAjax()) {
            $result = $this->model->save(['tuijian' => '1'], ['id' => $this->request->post('id')]);
            if ($result) {
                $this->success('修改成功！');
            } else {
                $this->error('没有修改！');
            }
        }
    }

    /**
     * 不推荐
     */
    public function NotRrecommended()
    {
        if ($this->request->isAjax()) {
            $result = $this->model->save(['tuijian' => '0'], ['id' => $this->request->post('id')]);
            if ($result) {
                $this->success('修改成功！');
            } else {
                $this->error('没有修改！');
            }
        }
    }

    /* 热销
     */
    public function hotup()
    {
        if ($this->request->isAjax()) {
            $result = $this->model->save(['hot' => '1'], ['id' => $this->request->post('id')]);
            if ($result) {
                $this->success('修改成功！');
            } else {
                $this->error('没有修改！');
            }
        }
    }

    /**
     * 取消热销
     */
    public function hotdu()
    {
        if ($this->request->isAjax()) {
            $result = $this->model->save(['hot' => '0'], ['id' => $this->request->post('id')]);
            if ($result) {
                $this->success('修改成功！');
            } else {
                $this->error('没有修改！');
            }
        }
    }
    /* 头条
     */
    public function topup()
    {
        if ($this->request->isAjax()) {
            $result = $this->model->save(['top' => '1'], ['id' => $this->request->post('id')]);
            if ($result) {
                $this->success('修改成功！');
            } else {
                $this->error('没有修改！');
            }
        }
    }

    /**
     * 取消头条
     */
    public function topdu()
    {
        if ($this->request->isAjax()) {
            $result = $this->model->save(['top' => '0'], ['id' => $this->request->post('id')]);
            if ($result) {
                $this->success('修改成功！');
            } else {
                $this->error('没有修改！');
            }
        }
    }

    /**
     * 全部操作
     */
    public function dolistsave()
    {
        $post = $this->request->post();
        if (empty($post['id'])) {
            return ajax_info('请选择项目！', 'index');
        }
        foreach ($post['id'] as $key) {
            $kk[] = $key . ',';
            switch ($post['inputtype']) {
                case 'move':
                    $data['cid'] = $post['cid'];
                    $keys['id'] = $key;
                    $this->model->update($data, $keys);
                    break;
                case 'del':
                    $this->model->where('id', $key)->delete();
                    break;
                case 'forbid':
                    $data['status'] = '0';
                    $keys['id'] = $key;
                    $this->model->update($data, $keys);
                    break;
                case 'resume':
                    $data['status'] = '1';
                    $keys['id'] = $key;
                    $this->model->update($data, $keys);
                    break;
                case 'fail':
                    $data['status'] = '2';
                    $keys['id'] = $key;
                    $this->model->update($data, $keys);
                    break;
            }
        }

        if ($post['inputtype'] == 'copy') {
            $kk = implode($kk);
            $kk = rtrim($kk, ',');
            switch (strtolower($this->request->controller())) {
                case 'article':
//                    $sql = 'insert into ' . Config('database.prefix') . 'article (`cid`,`uid`,`key`,`wechatkey`,`title`,`image`,`center`,`seo_title`,`miaoshu`,`sort`,`tag`,`hits`,`issue`,`status`,`lang`,`deleteTime`,`create_time`,`update_time`)
//            select REPLACE(`cid`,\'' . $post['cid'] . '\',\'' . $post['newcid'] . '\'),`uid`,`key`,`wechatkey`,`title`,`image`,`center`,`seo_title`,`miaoshu`,`sort`,`tag`,`hits`,`issue`,REPLACE(`status`,1,0),REPLACE(`lang`,\'' . cookie('think_var_admin') . '\',\'' . $post['lang'] . '\'),`deleteTime`,`create_time`,`update_time`
//            from ' . Config('database.prefix') . 'article where id in(' . $kk . ')';
                    $sql = 'insert into ' . Config('database.prefix') . 'article (`cid`,`uid`,`key`,`wechatkey`,`title`,`image`,`center`,`seo_title`,`miaoshu`,`sort`,`tag`,`hits`,`issue`,`status`,`lang`,`deleteTime`,`create_time`,`update_time`) 
            select REPLACE(`cid`,`cid`,\'' . $post['newcid'] . '\'),`uid`,`key`,`wechatkey`,`title`,`image`,`center`,`seo_title`,`miaoshu`,`sort`,`tag`,`hits`,`issue`,REPLACE(`status`,1,0),REPLACE(`lang`,\'' . cookie('think_var_admin') . '\',\'' . $post['lang'] . '\'),`deleteTime`,`create_time`,`update_time` 
            from ' . Config('database.prefix') . 'article where id in(' . $kk . ')';
                    break;
                case 'classroom':
                    break;
                case 'product':
//                    $sql = 'insert into ' . Config('database.prefix') . 'product (  `cid`,`uid`,`key`,`title`,`tuijian`,`jiaqian`,`image`,`xianqing`,`center`,`seo_title`,`miaoshu`,`parameter`,`point`,`exhibition`,`sort`,`tag`,`hits`,`issue`,`status`,`lang`,`deleteTime`,`create_time`,`update_time`)
//                    select   REPLACE(`cid`,\'' . $post['cid'] . '\',\'' . $post['newcid'] . '\'),`uid`,`key`,`title`,`tuijian`,`jiaqian`,`image`,`xianqing`,`center`,`seo_title`,`miaoshu`,`parameter`,`point`,`exhibition`,`sort`,`tag`,`hits`,`issue`,REPLACE(`status`,1,0),REPLACE(`lang`,\'' . cookie('think_var_admin') . '\',\'' . $post['lang'] . '\'),`deleteTime`,`create_time`,`update_time`
//                    from ' . Config('database.prefix') . 'product where id in(' . $kk . ')';
                    $sql = 'insert into ' . Config('database.prefix') . 'product (  `cid`,`uid`,`key`,`title`,`tuijian`,`jiaqian`,`image`,`xianqing`,`center`,`seo_title`,`miaoshu`,`parameter`,`point`,`exhibition`,`sort`,`tag`,`hits`,`issue`,`status`,`lang`,`deleteTime`,`create_time`,`update_time`) 
                    select   REPLACE(`cid`,`cid`,\'' . $post['newcid'] . '\'),`uid`,`key`,`title`,`tuijian`,`jiaqian`,`image`,`xianqing`,`center`,`seo_title`,`miaoshu`,`parameter`,`point`,`exhibition`,`sort`,`tag`,`hits`,`issue`,REPLACE(`status`,1,0),REPLACE(`lang`,\'' . cookie('think_var_admin') . '\',\'' . $post['lang'] . '\'),`deleteTime`,`create_time`,`update_time`
                    from ' . Config('database.prefix') . 'product where id in(' . $kk . ')';
                    break;
                case 'download':
                    $sql = 'insert into ' . Config('database.prefix') . 'download (`cid`,`lang`,`title`,`file`,`issue`,`size`,`status`,`value`,`miaoshu`,`sort`,`update_time`,`create_time`) 
                    select   REPLACE(`cid`,`cid`,\'' . $post['newcid'] . '\'),REPLACE(`lang`,\'' . cookie('think_var_admin') . '\',\'' . $post['lang'] . '\'),`title`,`file`,`issue`,`size`,`status`,`value`,`miaoshu`,`sort`,`update_time`,`create_time`
                    from ' . Config('database.prefix') . 'download where id in(' . $kk . ')';
                    break;
                case 'ucase':
                    $sql = 'insert into ' . Config('database.prefix') . 'ucase (`cid`,`lang`,`url`,`uid`,`key`,`title`,`jiaqian`,`image`,`xianqing`,`center`,`seo_title`,`miaoshu`,`sort`,`tag`,`hits`,`issue`,`status`,`deleteTime`,`create_time`,`update_time`,`QRcode`,`appsimage`) 
                    select   REPLACE(`cid`,`cid`,\'' . $post['newcid'] . '\'),REPLACE(`lang`,\'' . cookie('think_var_admin') . '\',\'' . $post['lang'] . '\'),`url`,`uid`,`key`,`title`,`jiaqian`,`image`,`xianqing`,`center`,`seo_title`,`miaoshu`,`sort`,`tag`,`hits`,`issue`,`status`,`deleteTime`,`create_time`,`update_time`,`QRcode`,`appsimage`
                    from ' . Config('database.prefix') . 'ucase where id in(' . $kk . ')';
                    break;
                case 'job':
                    break;
                case 'supply':
                    break;
                case 'purchase':
                    break;
                case 'tender':
                    break;
                default:
                    return ajax_info('没有！'.strtolower($this->request->controller()), 'index');
            }

            Db::execute($sql);

        }

        return ajax_info('操作成功！', 'index');

    }

    public function _html_empty()
    {
        $this->assign('_html_empty', '<tr><td colspan=\'100\'>暂无数据</td></tr>');
    }

    /**
     * 删除缓存
     */
    public function _delcahe()
    {
        cache('Forlayer' . cookie('think_var_admin'), null);
        cache('catetree' . cookie('think_var_admin'), null);
        cache('DB_TREE_ARETYPE' . cookie('think_var_admin').'index', null);
        cache('DB_TREE_ARETYPE' . cookie('think_var_admin').'mobile', null);
    }


}